package xs.szw.service.handler;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import xs.szw.common.result.ResultCode;
import xs.szw.common.utils.ResponseUtils;

import java.io.IOException;

/**
 * @program: My_SpringSecurity
 * @description: 认证异常处理
 * @author: Songzw
 * @create: 2025-02-01 13:54
 **/
@Component
@Slf4j
public class AuthenticationEntryPointImpl implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        log.warn("[commence][访问 URL({}) 时，没有登录]", request.getRequestURI(), authException);
//        Result<Object> fail = Result.fail(String.valueOf(HttpStatus.UNAUTHORIZED.value()), "认证失败重新登录！");
//        ServletUtils.renderString(response, JSON.toJSONString(fail),HttpStatus.UNAUTHORIZED.value());
        ResponseUtils.writeErrMsg(response, ResultCode.CLIENT_AUTHENTICATION_FAILED);
    }
}
